package com.ltf.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * @Auther: liutf
 * @Date: 2025/4/12
 * @Description: com.ltf.gateway.filter
 * @version: 1.0
 */

@Component
@Slf4j
public class RtGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        long startTime = System.currentTimeMillis();
        URI uri = exchange.getRequest().getURI();
        log.info("请求【{}】开始，时间：{}", uri, startTime);

        return chain.filter(exchange).doFinally((result)->{
            long endTime = System.currentTimeMillis();
            log.info("请求【{}】结束，时间：{}，耗时：{}ms", uri, endTime,endTime-startTime);
        });
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
